package com.springboot_gradrecruit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.mapper.InterviewMapper;
import com.springboot_gradrecruit.mapper.JobMapper;
import com.springboot_gradrecruit.model.dto.interview.PageInterviewRequest;
import com.springboot_gradrecruit.model.entity.Interview;
import com.springboot_gradrecruit.model.entity.Job;
import com.springboot_gradrecruit.model.vo.interview.InterviewVO;
import com.springboot_gradrecruit.service.InterviewService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author zr
 * @description 针对表【new】的数据库操作Service实现
 * @createDate 2024-12-09 14:40:37
 */
@Service
public class InterviewServiceImpl extends ServiceImpl<InterviewMapper, Interview>
        implements InterviewService {

    @Resource
    private InterviewMapper interviewMapper;

    @Resource
    private JobMapper jobMapper;

    @Override
    public PageResponse<InterviewVO> pageInterview(PageInterviewRequest pageInterviewRequest) {
        //1.获取参数
        Long interviewStudentId = pageInterviewRequest.getInterviewStudentId();

        if (interviewStudentId == null) {
            return PageResponse.of(new Page<>(), InterviewVO.class);
        }

        //2.构造查询条件
        Page<Interview> page = pageInterviewRequest.toPageDefaultSortByUpdateTime();

        //3.执行查询
        List<InterviewVO> interviewVOList = interviewMapper.getInterviewWithJobById(interviewStudentId);
        interviewVOList.forEach(interviewVO -> {
            if("未面试".equals(interviewVO.getInterviewStatus())){
                Date  now = new Date() ;
                Date interviewDate = interviewVO.getInterviewData();
                if(now.after(interviewDate)){
                    QueryWrapper<Interview> interviewQueryWrapper = new QueryWrapper<>();
                    interviewQueryWrapper.eq("interview_id",interviewVO.getInterviewId());
                    Interview interview = interviewMapper.selectOne(interviewQueryWrapper);
                    interview.setInterviewStatus("已面试");
                    interviewMapper.updateById(interview);
                }
            }
        });
        //4.查询面试-求职意向的面试
        QueryWrapper<Interview> interviewQueryWrapper = new QueryWrapper<>();
        interviewQueryWrapper.eq("interview_student_id", interviewStudentId);
        interviewQueryWrapper.isNull("interview_job_id");

        List<Interview> interviewVOList2 = interviewMapper.selectList(interviewQueryWrapper);
        //5.合并两个List
        for(Interview interview:interviewVOList2){
            // 获取当前时间（使用LocalDateTime来表示当前时间）
            if("未面试".equals(interview.getInterviewStatus())){
                Date  now = new Date() ;
                Date interviewDate = interview.getInterviewData();
                if(now.after(interviewDate)){
                    interview.setInterviewStatus("已面试");
                    interviewMapper.updateById(interview);
                }
            }
            interviewVOList.add(BeanUtil.copyProperties(interview,InterviewVO.class));
        }
        String interviewStatus = pageInterviewRequest.getInterviewStatus();
        if (interviewStatus != null) {
            interviewVOList = interviewVOList.stream()
                    .filter(interviewVO -> interviewVO.getInterviewStatus().equals(interviewStatus))
                    .toList();
        }

        System.out.println("查询结果:" + interviewVOList);
        return PageResponse.of(page, interviewVOList);
    }

    @Override
    public PageResponse<InterviewVO> pageInterviewByEnterprise(PageInterviewRequest pageInterviewRequest) {
        //1.获取参数
        Long interviewEnterpriseId = pageInterviewRequest.getInterviewEnterpriseId();
        if (interviewEnterpriseId == null) {
            return PageResponse.of(new Page<>(), InterviewVO.class);
        }
        //2.构造查询条件
        Page<Interview> page = pageInterviewRequest.toPageDefaultSortByUpdateTime();

        //3.执行查询
        List<InterviewVO> interviewVOList = interviewMapper.getInterviewWithJobByIdByEnterpriseId(interviewEnterpriseId);
        for(InterviewVO interviewVO:interviewVOList){
            if("未面试".equals(interviewVO.getInterviewStatus())){
                Date  now = new Date() ;
                Date interviewDate = interviewVO.getInterviewData();
                if(now.after(interviewDate)){
                    QueryWrapper<Interview> interviewQueryWrapper = new QueryWrapper<>();
                    interviewQueryWrapper.eq("interview_id",interviewVO.getInterviewId());
                    Interview interview = interviewMapper.selectOne(interviewQueryWrapper);
                    interview.setInterviewStatus("已面试");
                    interviewMapper.updateById(interview);
                }
            }
        }
        //4.查询面试-求职意向的面试
        QueryWrapper<Interview> interviewQueryWrapper = new QueryWrapper<>();
        interviewQueryWrapper.eq("interview_enterprise_id", interviewEnterpriseId);
        interviewQueryWrapper.isNull("interview_job_id");
        interviewQueryWrapper.eq("enterprise_is_deleted","未删除");
        List<Interview> interviewVOList2 = interviewMapper.selectList(interviewQueryWrapper);
        for(Interview interview:interviewVOList2){
            // 获取当前时间（使用LocalDateTime来表示当前时间）
            if("未面试".equals(interview.getInterviewStatus())){
                Date  now = new Date() ;
                Date interviewDate = interview.getInterviewData();
                if(now.after(interviewDate)){
                    interview.setInterviewStatus("已面试");
                    interviewMapper.updateById(interview);
                }
            }
            interviewVOList.add(BeanUtil.copyProperties(interview,InterviewVO.class));
        }
        String interviewStatus = pageInterviewRequest.getInterviewStatus();
        if (interviewStatus != null) {
            interviewVOList = interviewVOList.stream()
                    .filter(interviewVO -> interviewVO.getInterviewStatus().equals(interviewStatus))
                    .toList();
        }
        System.out.println("查询结果:" + interviewVOList);
        return PageResponse.of(page, interviewVOList);
    }

    @Override
    public InterviewVO queryInterviewById(Long interviewId) {
        //1.查询出面试信息
        QueryWrapper<Interview> interviewQueryWrapper = new QueryWrapper<>();
        interviewQueryWrapper.eq("interview_id",interviewId);
        Interview interview = interviewMapper.selectOne(interviewQueryWrapper);

        InterviewVO interviewVO = BeanUtil.copyProperties(interview,InterviewVO.class);

        //2.更加面试id查询出对应的职位信息
        if(interview.getInterviewJobId()!=null){
            QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();
            jobQueryWrapper.eq("job_id",interview.getInterviewJobId());
            Job job = jobMapper.selectOne(jobQueryWrapper);
            interviewVO.setJobCompanyName(job.getJobCompanyName());
            interviewVO.setJobTitle(job.getJobTitle());
            interviewVO.setJobSalary(job.getJobSalary());
            interviewVO.setJobEducationRequirement(job.getJobEducationRequirement());
            interviewVO.setJobMajorRequirement(job.getJobMajorRequirement());
            interviewVO.setJobCompanySize(job.getJobCompanySize());
            interviewVO.setJobCompanyLogo(job.getJobCompanyLogo());
        }
        return interviewVO;
    }

    @Override
    public boolean updateInterviewIsOffer(Long interviewId, String interviewIsOffer) {
        LambdaUpdateWrapper<Interview> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Interview::getInterviewId, interviewId)
                .set(Interview::getInterviewIsOffer, interviewIsOffer);
        return interviewMapper.update(null, updateWrapper) > 0;
    }
}




